#ifndef _PowerWindow_h_
#define _PowerWindow_h_
 
#include <CtrlLib/CtrlLib.h>

#include "project_shared.h"

using namespace Upp;

#define DIMENSION_TAG "dimensions"

//============================================================================================================================
class PowerWindow : public TopWindow {
private:
	Config *config;
	bool configured;
public:
	//============================================================================================================================
	PowerWindow() { // Called from WithLayout template
		
		configured = false;
		config = NULL;
	}
	
	//============================================================================================================================
	~PowerWindow() {
		
		if (config) {
			DELETE_OB(config);
		}
	}
	
	inline String getConfigFileBase(String windowName) {
		return ConfigFile(windowName);
	}
	
	inline String getTextTagConfigFile(String windowName) {
		return CAT << getConfigFileBase(CAT << windowName << ".xml");
	}

	inline String getBinTagConfigFile(String windowName) {
		return CAT << getConfigFileBase(CAT << windowName << ".bin");
	}

	//============================================================================================================================
	void LoadConfig(String windowName) {
		LoadFromFile(*this, getBinTagConfigFile(windowName));
		LoadFromXMLFile(*this, getTextTagConfigFile(windowName));
	}

	//============================================================================================================================
	void SaveConfig(String windowName) {
		StoreToFile(*this, getBinTagConfigFile(windowName));
		StoreAsXMLFile(*this, NULL, getTextTagConfigFile(windowName));
	}
	
	// Sadly, TopWindow::Open is not virtual, so in TopWindow::Run it will not call this function, so we have to duplicate
	//============================================================================================================================
	void Open(String windowName) {
		TopWindow::Open();
		LoadConfig(windowName);
	}
	
	//============================================================================================================================
	void Close(String windowName) {
		SaveConfig(windowName);
		TopWindow::Close();  // Won't close unless this is called
	}

	//============================================================================================================================
	int Run(String windowName, bool appmodal = false) { // Internally calls TopWindow::Open, not our Open
		LoadConfig(windowName);
		return TopWindow::Run(appmodal);
	}
	
	//============================================================================================================================
	void Serialize(Stream &s) {
		SerializePlacement(s);
	}
	
	//============================================================================================================================
	virtual void Xmlize(XmlIO xml) = 0;	// Force implementers to create an Xmlize, as U++ should have done.
};

#endif
